package com.sunwayworld.basemodule.business.folder.validators;

import com.sunwayworld.basemodule.business.ordertask.bean.LimsOrderTaskBean;
import com.sunwayworld.basemodule.business.ordertask.service.LimsOrderTaskService;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.validator.data.DataValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 质控样删除校验
 */
@Repository
@GikamBean
public class LimsQCSampleDeleteValidator implements DataValidator {

    // 删除数据的 service
    @Autowired
    private LimsOrderTaskService orderTaskService;

    @Override
    public boolean doValid(Object... args) {
        RestJsonWrapperBean wrapper = (RestJsonWrapperBean) args[0];
        boolean result = true;
        // 需要删除的数据 id
        List<LimsOrderTaskBean> orderTaskBeans = wrapper.parse(LimsOrderTaskBean.class);
        List<Long> idList = orderTaskBeans.stream().map(LimsOrderTaskBean::getFolderId).collect(Collectors.toList());
        // 查看该质控任务下 是否有已完成的工序，有则无法删除
        List<LimsOrderTaskBean> taskBeans = orderTaskService.selectListByFilter(SearchFilter.instance()
                .match("folderId", idList).filter(MatchPattern.OR)
                .match("status", "draft").filter(MatchPattern.DIFFER));
        if (!taskBeans.isEmpty()) {
            this.addConstraintViolation(I18nHelper.getMessage("MODULE.BUSINESS.ORDERTASK.VALIDATOR.DELETE_QC_SAMPLE"));
            result = false;
        }
        return result;
    }
}
